package com.hiwhitley.himei;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.hiwhitley.himei.app.base.BaseActivity;
import com.hiwhitley.himei.module.game.GameListFragment;
import com.hiwhitley.himei.module.gank.GankFragment;
import com.hiwhitley.himei.module.me.MeFragment;
import com.hiwhitley.himei.module.meitu.MeiTuFragment;
import com.hiwhitley.himei.module.video.VideoFragment;
import com.hiwhitley.himei.utils.StrUtil;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements MeFragment.ShowTabBottomListener {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar mBottomNavigationBar;

    private MeiTuFragment mMeiTuFragment;
    private VideoFragment mVideoFragment;
    private GameListFragment mGameListFragment;
    private GankFragment mMeFragment;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void init() {

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.line, StrUtil.getFromRes(this, R.string.nv_photo)))
                .addItem(new BottomNavigationItem(R.drawable.ic_video, StrUtil.getFromRes(this, R.string.nv_video)))
                .addItem(new BottomNavigationItem(R.drawable.ic_game, StrUtil.getFromRes(this, R.string.nv_game)))
                .addItem(new BottomNavigationItem(R.drawable.ic_about, StrUtil.getFromRes(this, R.string.nv_about)))
                .initialise();

        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position) {
                    case 0:
                        showFragment(0);
                        break;
                    case 1:
                        showFragment(1);
                        break;
                    case 2:
                        showFragment(2);
                        break;
                    case 3:
                        showFragment(3);
                        break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });

    }


    @Override
    protected void setListeners() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            mMeiTuFragment = (MeiTuFragment) getSupportFragmentManager().findFragmentByTag(MeiTuFragment.class.getName());
            mVideoFragment = (VideoFragment) getSupportFragmentManager().findFragmentByTag(VideoFragment.class.getName());
            mGameListFragment = (GameListFragment) getSupportFragmentManager().findFragmentByTag(GameListFragment.class.getName());
            mMeFragment = (GankFragment) getSupportFragmentManager().findFragmentByTag(GankFragment.class.getName());
        }

        showFragment(0);

    }

    private void showFragment(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideAllFragment(ft);
        switch (position) {
            case 0:
                if (mMeiTuFragment != null) {
                    ft.show(mMeiTuFragment);
                } else {
                    mMeiTuFragment = new MeiTuFragment();
                    ft.add(R.id.id_content, mMeiTuFragment, MeiTuFragment.class.getName());
                }
                break;
            case 1:
                if (mVideoFragment != null) {
                    ft.show(mVideoFragment);
                } else {
                    mVideoFragment = new VideoFragment();
                    ft.add(R.id.id_content, mVideoFragment, VideoFragment.class.getName());
                }
                break;
            case 2:
                if (mGameListFragment != null) {
                    ft.show(mGameListFragment);
                } else {
                    mGameListFragment = new GameListFragment();
                    ft.add(R.id.id_content, mGameListFragment, GameListFragment.class.getName());
                }
                break;
            case 3:
            default:
                if (mMeFragment != null) {
                    ft.show(mMeFragment);
                } else {
                    mMeFragment = new GankFragment();
                    ft.add(R.id.id_content, mMeFragment, GankFragment.class.getName());
                }
                break;
        }
        ft.commit();
    }

    private void hideAllFragment(FragmentTransaction ft) {
        if (mMeiTuFragment != null) {
            ft.hide(mMeiTuFragment);
        }
        if (mVideoFragment != null) {
            ft.hide(mVideoFragment);
        }

        if (mGameListFragment != null) {
            ft.hide(mGameListFragment);
        }

        if (mMeFragment != null) {
            ft.hide(mMeFragment);
        }

    }


    @Override
    public void toggleTabBottom(boolean isShow) {
        if (isShow)
            mBottomNavigationBar.show();
        else
            mBottomNavigationBar.hide();
    }
}
